package javaJob.exercise.primary.array;


//可以用哈希表遍历判断元素是否重复
//直接遍历

public class primary9 {
    public static void main(String[] args) {
        char[][] board =
                {{'8','3','.','.','7','.','.','.','.'}
                ,{'6','.','.','1','9','5','.','.','.'}
                ,{'.','9','8','.','.','.','.','6','.'}
                ,{'8','.','.','.','6','.','.','.','3'}
                ,{'4','.','.','8','.','3','.','.','1'}
                ,{'7','.','.','.','2','.','.','.','6'}
                ,{'.','6','.','.','.','.','2','8','.'}
                ,{'.','.','.','4','1','9','.','.','5'}
                ,{'.','.','.','.','8','.','.','7','9'}};
        char[][] board1 =
                {{'5','3','.','.','7','.','.','.','.'}
                ,{'6','.','.','1','9','5','.','.','.'}
                ,{'.','9','8','.','.','.','.','6','.'}
                ,{'8','.','.','.','6','.','.','.','3'}
                ,{'4','.','.','8','.','3','.','.','1'}
                ,{'7','.','.','.','2','.','.','.','6'}
                ,{'.','6','.','.','.','.','2','8','.'}
                ,{'.','.','.','4','1','9','.','.','5'}
                ,{'.','.','.','.','8','.','.','7','9'}};

//        [["5","3",".",".","7",".",".",".","."],
//        ["6",".",".","1","9","5",".",".","."],
//        [".","9","8",".",".",".",".","6","."],
//        ["8",".",".",".","6",".",".",".","3"],
//        ["4",".",".","8",".","3",".",".","1"],
//        ["7",".",".",".","2",".",".",".","6"],
//        [".","6",".",".",".",".","2","8","."],
//        [".",".",".","4","1","9",".",".","5"],
//        [".",".",".",".","8",".",".","7","9"]]

        System.out.println(isValidSudoku(board1));
    }

    static boolean isValidSudoku(char[][] board) {
        boolean answer=true;
        for (int i=0;i<board.length;i++){
            for (int l=0;l<9;l++){
                for (int k=l+1;k<9;k++){
                    if ('.'!=board[i][l] & board[i][l]==board[i][k]){
                        answer=false;
                        System.out.println("board[i][l]:"+board[i][l]);
                        System.out.println("board[i][k]"+board[i][k]);
                        System.out.println("进入判断");
                    }
                }
            }

            System.out.println("行遍历正确"+" i:"+i+answer);

            for (int l=0;l<9;l++){
                for (int k=l+1;k<9;k++){
                    if ('.'!=board[l][i] & board[l][i]==board[k][i]){
                        System.out.println("l i:"+board[l][i]);
                        System.out.println("k i:"+board[k][i]);
                        answer=false;
                    }
                }
            }

            System.out.println("列遍历正确"+" i:"+i+answer);

            if (i%3==0){

                System.out.println("进入判断1");

                for (int l=0;l<9;l++){
                    if ('.'!=board[i][l] & l%3==0){
                        if(board[i+1][l+1]==board[i][l] | board[i+2][l+2]==board[i][l] | board[i+1][l+2]==board[i][l] | board[i+2][l+1]==board[i][l]){
                            answer=false;
                            System.out.println("出错1"+board[i][l]);
                        }
                    }else if ('.'!=board[i][l] & l%3==1){
                        if (board[i+1][l+1]==board[i][l] | board[i+2][l+1]==board[i][l] | board[i+2][l-1]==board[i][l] | board[i+1][l-1]==board[i][l]){
                            answer=false;
                            System.out.println("出错2"+board[i][l]);
                        }
                    }else if ('.'!=board[i][l] & l%3==2){
                        if (board[i+1][l-1]==board[i][l] | board[i+1][l-2]==board[i][l] | board[i+2][l-1]==board[i][l] | board[i+2][l-2]==board[i][l]){
                            answer=false;
                            System.out.println("出错3"+board[i][l]);
                        }
                    }
                }
            }else if (i%3==1){

                System.out.println("进入判断2");

                for (int l=0;l<9;l++){
                    if ('.'!=board[i][l] & l%3==0){
                        if(board[i+1][l+1]==board[i][l] | board[i-1][l+2]==board[i][l] | board[i+1][l+2]==board[i][l] | board[i-1][l+1]==board[i][l]){
                            answer=false;
                            System.out.println("出错4"+board[i][l]);
                        }
                    }else if ('.'!=board[i][l] & l%3==1){
                        if (board[i+1][l+1]==board[i][l] | board[i-1][l+1]==board[i][l] | board[i-1][l-1]==board[i][l] | board[i+1][l-1]==board[i][l]){
                            answer=false;
                            System.out.println("出错5"+board[i][l]);
                        }
                    }else if ('.'!=board[i][l] & l%3==2){
                        if (board[i-1][l-1]==board[i][l] | board[i-1][l-2]==board[i][l] | board[i+1][l-1]==board[i][l] | board[i+1][l-2]==board[i][l]){
                            answer=false;
                            System.out.println("出错6"+board[i][l]);
                        }
                    }
                }
            }else if (i%3==2){

                System.out.println("进入判断3");

                for (int l=0;l<9;l++){
                    if ('.'!=board[i][l] & l%3==0){
                        System.out.println("---1");
                        if(board[i-2][l+1]==board[i][l] | board[i-1][l+2]==board[i][l] | board[i-2][l+2]==board[i][l] | board[i-1][l+1]==board[i][l]){
                            answer=false;
                            System.out.println("出错7"+board[i][l]);
                        }
                    }else if ('.'!=board[i][l] & l%3==1){
                        System.out.println("---2");
                        if (board[i-2][l+1]==board[i][l] | board[i-1][l+1]==board[i][l] | board[i-2][l-1]==board[i][l] | board[i-1][l-1]==board[i][l]){
                            answer=false;
                            System.out.println("出错8"+board[i][l]);
                        }
                    }else if ('.'!=board[i][l] & l%3==2){
                        System.out.println("--3");
                        if (board[i-1][l-1]==board[i][l] | board[i-1][l-2]==board[i][l] | board[i-2][l-1]==board[i][l] | board[i-2][l-2]==board[i][l]){
                            answer=false;
                            System.out.println("出错9"+board[i][l]);
                        }
                    }
                }
            }else {
                answer=true;
            }
//            else if (i>=3 & i<=5){
//                for (int l=0;l<9;l++){
//                    for (int k=0;k<9;k++){
//                        if (board[i][l]==board[i][k]){
//                            answer=false;
//                        }
//                    }
//                }
//            }else if (i>=6 & i<=9){
//                for (int l=0;l<9;l++){
//                    for (int k=0;k<9;k++){
//                        if (board[i][l]==board[i][k]){
//                            answer=false;
//                        }
//                    }
//                }
//            }
        }

        return answer;
    }
}
